/**
 * Screen class
 */
package com.frontend.screens;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JPanel;

import com.frontend.DoctorsOfficeClient;

/**
 * The Class Screen.
 * 
 * @author Elizabeth
 * @author Taylor
 */
public class Screen extends JPanel {

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 4482490526778384692L;

	/** The window. */
	protected DoctorsOfficeClient window;

	/** The tempheight. */
	protected int tempwidth, tempheight;

	private int hueShift;

	/**
	 * Instantiates a new screen.
	 * 
	 * @param window
	 *            the window
	 */
	public Screen(DoctorsOfficeClient window) {
		this.window = window;
		tempwidth = 0;
		tempheight = 0;
		if (window.getCurrentUser() != null
				&& window.getCurrentUser().getName().toLowerCase()
						.contains("rainbow")) {
			javax.swing.Timer timer = new javax.swing.Timer(50,
					new RainbowListener());
			hueShift = 0;
			timer.start();
		}
	}

	/**
	 * Instantiates a new screen.
	 * 
	 * @param arg0
	 *            the arg0
	 */
	public Screen(LayoutManager arg0) {
		super(arg0);
	}

	/**
	 * Instantiates a new screen.
	 * 
	 * @param arg0
	 *            the arg0
	 */
	public Screen(boolean arg0) {
		super(arg0);
	}

	/**
	 * Instantiates a new screen.
	 * 
	 * @param arg0
	 *            the arg0
	 * @param arg1
	 *            the arg1
	 */
	public Screen(LayoutManager arg0, boolean arg1) {
		super(arg0, arg1);
	}

	/**
	 * Clear temps.
	 */
	public void clearTemps() {
		tempheight = 0;
		tempwidth = 0;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
	 */
	public void paintComponent(Graphics g) {
		Graphics2D g2d = (Graphics2D) g;

		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);

		if (window.getCurrentUser() != null
				&& window.getCurrentUser().getName().toLowerCase()
						.contains("rainbow")) {
			for (int i = 0; i < getHeight(); i++) {
				int hue = ((i * 360 / getHeight()) + hueShift)%360;
				g.setColor(HSVtoRGB(hue, 100, 100));
				g.drawLine(0, i, getWidth(), i);
			}
		} else {
			for (int i = 0; i < getHeight(); i++) {
				int offset = 70 * i / getHeight();
				g.setColor(new Color(130 + offset, 160 + offset, 255));
				g.drawLine(0, i, getWidth(), i);
			}
		}
	}

	private class RainbowListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent arg0) {
			hueShift = ((hueShift-2)+360) % 360;
			repaint();
		}

	}
	
	static protected String[] getMonthStrings() {
        String[] months = new java.text.DateFormatSymbols().getMonths();
        int lastIndex = months.length - 1;

        if (months[lastIndex] == null
           || months[lastIndex].length() <= 0) { //last item empty
            String[] monthStrings = new String[lastIndex];
            System.arraycopy(months, 0,
                             monthStrings, 0, lastIndex);
            return monthStrings;
        } else { //last item not empty
            return months;
        }
    }
	
	static protected String[] getTimesArray(){
		String[] times = new String[48];
		for(int i = 0; i < 48; i++){
			times[i] = "";
			if(i < 20)
				times[i] += "0";
			times[i] += (i / 2);
			times[i] += ":";
			if((i & 1) == 1)
				times[i] += "30";
			else
				times[i] += "00";
		}
		return times;
	}
	
	private Color HSVtoRGB(int hue, int sat, int val) {
		float red = 0;
		float green = 0;
		float blue = 0;
		int hPrime = hue / 60;
		float s = (float) sat / 100;
		float v = (float) val / 100;
		float chroma = 0;
		float x = 0;
		float m = 0;

		if (sat < 0)
			return Color.white;

		chroma = v * s;
		x = chroma * (hue % 60) / 60;

		switch (hPrime) {
		case 0:
			red = chroma;
			green = x;
			blue = 0;
			break;
		case 1:
			red = chroma - x;
			green = chroma;
			blue = 0;
			break;
		case 2:
			red = 0;
			green = chroma;
			blue = x;
			break;
		case 3:
			red = 0;
			green = chroma - x;
			blue = chroma;
			break;
		case 4:
			red = x;
			green = 0;
			blue = chroma;
			break;
		case 5:
			red = chroma;
			green = 0;
			blue = chroma - x;
			break;
		default:
			red = 0;
			green = 0;
			blue = 0;
		}

		m = v - chroma;

		return new Color((int) ((red + m) * 255),
				(int) ((green + m) * 255), (int) ((blue + m) * 255));
	}
}
